using System.Text.Json;
using System.Text.Json.Serialization;

namespace Sage.Http.Factory
{
    /// <summary>
    /// JSON序列化选项工厂，负责创建和管理JSON序列化配置
    /// </summary>
    public static class JsonSerializerOptionsFactory
    {
        /// <summary>
        /// 全局JSON序列化选项
        /// </summary>
        private static JsonSerializerOptions? _globalOptions;

        /// <summary>
        /// 全局JSON序列化上下文
        /// </summary>
        private static JsonSerializerContext? _globalContext;

        /// <summary>
        /// 线程安全锁
        /// </summary>
        private static readonly Lock _lock = new();

        /// <summary>
        /// 获取全局JSON序列化选项
        /// </summary>
        /// <returns>全局JSON序列化选项，如果未设置则返回null</returns>
        public static JsonSerializerOptions? GetGlobalOptions()
        {
            lock (_lock)
            {
                return _globalOptions;
            }
        }

        /// <summary>
        /// 设置全局JSON序列化选项
        /// </summary>
        /// <param name="options">要设置的JSON序列化选项</param>
        public static void SetGlobalOptions(JsonSerializerOptions? options)
        {
            lock (_lock)
            {
                _globalOptions = options;
            }
        }

        /// <summary>
        /// 获取全局JSON序列化上下文
        /// </summary>
        /// <returns>全局JSON序列化上下文，如果未设置则返回null</returns>
        public static JsonSerializerContext? GetGlobalContext()
        {
            lock (_lock)
            {
                return _globalContext;
            }
        }

        /// <summary>
        /// 设置全局JSON序列化上下文
        /// </summary>
        /// <param name="context">要设置的JSON序列化上下文</param>
        public static void SetGlobalContext(JsonSerializerContext? context)
        {
            lock (_lock)
            {
                _globalContext = context;
            }
        }

        /// <summary>
        /// 创建默认的JSON序列化选项
        /// </summary>
        /// <returns>配置好的JSON序列化选项</returns>
        public static JsonSerializerOptions CreateDefaultOptions()
        {
            return new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                PropertyNameCaseInsensitive = true,
                DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
                WriteIndented = false,
                AllowTrailingCommas = true,
                ReadCommentHandling = JsonCommentHandling.Skip
            };
        }

        /// <summary>
        /// 创建Web API友好的JSON序列化选项
        /// </summary>
        /// <returns>配置好的JSON序列化选项</returns>
        public static JsonSerializerOptions CreateWebApiOptions()
        {
            var options = CreateDefaultOptions();

            // 使用内置的枚举命名策略，AOT友好
            options.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
            options.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;

            return options;
        }
        /// <summary>
        /// 创建用于文件存储的JSON序列化选项
        /// </summary>
        /// <returns>配置好的JSON序列化选项</returns>
        public static JsonSerializerOptions CreateFileStorageOptions()
        {
            var options = CreateDefaultOptions();
            options.WriteIndented = true; // 文件存储时使用缩进格式
            return options;
        }

        /// <summary>
        /// 获取有效的JSON序列化选项
        /// </summary>
        /// <param name="options">指定的选项</param>
        /// <returns>有效的JSON序列化选项</returns>
        /// <remarks>
        /// 优先级：指定选项 > 全局选项 > 默认选项
        /// </remarks>
        public static JsonSerializerOptions GetEffectiveOptions(JsonSerializerOptions? options = null)
        {
            return options ?? GetGlobalOptions() ?? CreateDefaultOptions();
        }

        /// <summary>
        /// 获取有效的JSON序列化上下文
        /// </summary>
        /// <param name="context">指定的上下文</param>
        /// <returns>有效的JSON序列化上下文，如果没有则返回null</returns>
        /// <remarks>
        /// 优先级：指定上下文 > 全局上下文
        /// </remarks>
        public static JsonSerializerContext? GetEffectiveContext(JsonSerializerContext? context = null)
        {
            return context ?? GetGlobalContext();
        }

        /// <summary>
        /// 重置全局配置
        /// </summary>
        public static void ResetGlobalConfiguration()
        {
            lock (_lock)
            {
                _globalOptions = null;
                _globalContext = null;
            }
        }
    }
}